package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ISmsHomeNewProductService;
import com.itheima.sms.entity.SmsHomeNewProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {

    @Autowired
    private ISmsHomeNewProductService smsHomeNewProductService;

    /**
     * 添加首页新品
     * @param homeNewProductList
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody List<SmsHomeNewProduct> homeNewProductList) {
        smsHomeNewProductService.saveBatch(homeNewProductList);
        return AjaxResult.success();
    }

    /**
     * 批量删除首页新品
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam List<Long> ids) {
        smsHomeNewProductService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 分页查询首页新品
     *
     * @param productName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "productName", required = false) String productName,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @RequestParam(value = "recommendStatus", required = false) Integer recommendStatus) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(productName != null, SmsHomeNewProduct::getProductName, productName)
                .eq(recommendStatus != null, SmsHomeNewProduct::getRecommendStatus, recommendStatus) .orderByDesc(SmsHomeNewProduct::getSort);

        smsHomeNewProductService.page(page, lqw);
        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);

    }

    /**
     * 批量修改首页新品状态
     *
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public AjaxResult updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam("recommendStatus") String recommendStatus) {
        LambdaUpdateWrapper<SmsHomeNewProduct> luw = new LambdaUpdateWrapper<>();
        luw.in(SmsHomeNewProduct::getId, ids)
                .set(SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        smsHomeNewProductService.update(luw);
        return AjaxResult.success();
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public AjaxResult updateSort(@PathVariable Long id,  @RequestParam("sort") Integer sort, @RequestParam("id") Integer ids) {
        LambdaUpdateWrapper<SmsHomeNewProduct> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsHomeNewProduct::getId, id)
                .set( SmsHomeNewProduct::getSort,sort);

        smsHomeNewProductService.update(luw);
        return AjaxResult.success();
    }
}
